package com.demo.serviceimpl;

import com.demo.bean.Lesson;
import org.springframework.stereotype.Service;
import com.demo.service.AnalysisHtmlService;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 李天航
 */
@Service
public class AnalysisHtmlServiceImpl implements AnalysisHtmlService {

    String beginTag1="<!--04 课程表及教学任务-->";
    String beginTag2="<div class=\"table-inner table-class\" id=\"bzkb\">";
    String beginTag3="<tbody class=\"table-class-even\">";
    String endTagTbody="</tbody>";
    String tdStyleBegin="<tdstyle=\"text-align:center;font-size:12px;\">";
    String tdEnd="</td>";
    String lessonMessageBegin="\"href=\"http://whut.fysso.chaoxing.com/sso/whutlogin\"target=\"_blank\">";
    String lessonMessageEnd="</a>";
    String tagP="<p>";
    String tagPEnd="</p>";


    @Override
    public String deleteAllBlank(String text){
        StringBuilder stringBuilder=new StringBuilder(text);
        for(int i=stringBuilder.length()-1;i>=0;i--){
            char c= stringBuilder.charAt(i);
            if(c==' '||c=='\t'||c=='\r'||c=='\n'){
                stringBuilder.deleteCharAt(i);
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 从一行中分离出课程
     *
     * @param text 一行文本
     * @return 处理好的课程
     */
    @Override
    public List<String> getTablesFromTr(String text) {
        List<String> tds=new ArrayList<>();
        Integer be=0,ed=0;
        for(int i=0;i<7;i++){
            be=text.indexOf(tdStyleBegin,ed);
            ed=text.indexOf(tdEnd,be);
            tds.add(deleteAllBlank(text.substring(be+tdStyleBegin.length(),ed)));
        }
        return tds;
    }

    void setTimesFromMsg(String timeMessage, Lesson lesson){
        if(timeMessage.contains("1-2")){
            lesson.setBeginTime(480L);
            lesson.setEndTime(575L);
        }else if(timeMessage.contains("3-4")){
            lesson.setBeginTime(595L);
            lesson.setEndTime(690L);
        }else if(timeMessage.contains("3-5")){
            lesson.setBeginTime(595L);
            lesson.setEndTime(740L);
        }else if(timeMessage.contains("6-7")){
            lesson.setBeginTime(840L);
            lesson.setEndTime(935L);
        }else if(timeMessage.contains("6-8")){
            lesson.setBeginTime(840L);
            lesson.setEndTime(985L);
        }else if(timeMessage.contains("9-10")){
            lesson.setBeginTime(1005L);
            lesson.setEndTime(1100L);
        }else if(timeMessage.contains("11-12")){
            lesson.setBeginTime(1140L);
            lesson.setEndTime(1235L);
        }else if(timeMessage.contains("11-13")){
            lesson.setBeginTime(1140L);
            lesson.setEndTime(1285L);
        }
    }

    /**
     * 从tr标签中分离出课程表的各种信息
     *
     * @param text 需要解析的文本
     * @return 课程
     */
    @Override
    public Lesson getLessonFromDiv(String text) {
        if(text==null|| "".equals(text)) {
            return new Lesson();
        }
        int be=0,ed=0;
        be=text.indexOf(lessonMessageBegin,ed);
        ed=text.indexOf(lessonMessageEnd,be);
        String lessonMessage=text.substring(be+lessonMessageBegin.length(),ed);
        int firstP=lessonMessage.indexOf(tagP);
        int nextPEnd=lessonMessage.indexOf(tagPEnd,firstP);
        String lessonName=lessonMessage.substring(0,firstP);
        String lessonLocation=lessonMessage.substring(firstP+tagP.length(),nextPEnd);
        int secondP=lessonMessage.indexOf(tagP,nextPEnd);
        int secondPEnd=lessonMessage.indexOf(tagPEnd,secondP);
        String timeMessage=lessonMessage.substring(secondP+tagP.length(),secondPEnd);
        Lesson lesson= Lesson.builder().lessonName(lessonName).lessonLocation(lessonLocation).build();
        setTimesFromMsg(timeMessage,lesson);
        return lesson;
    }

    /**
     * 从html文本获取课表
     *
     * @param text html文本
     * @return 课程
     */
    @Override
    public List<Lesson> getLessonsFromText(String text) {
        List<String> trs=getTrsFromTbody(text);
        List<Lesson> lessons=new ArrayList<>();
        for(int i=0;i<5;i++){
            List<String> tables=getTablesFromTr(trs.get(i));
            for(int j=0;j<7;j++){
                if(tables.get(j).equals("")||tables.get(j)==null) {
                    continue;
                }
                Lesson lesson=getLessonFromDiv(tables.get(j));
                lesson.setBeginTime(lesson.getBeginTime()+j*60*24L);
                lesson.setEndTime(lesson.getEndTime()+j*60*24L);
                lesson.setLessonType("学习");
                lessons.add(lesson);
            }
        }
        lessons.sort(((o1, o2) -> Long.compare(o1.getBeginTime(),o2.getBeginTime())));
        return lessons;
    }

    /**
     * 从tbody标签中分离tr元素
     *
     * @param text html文本
     * @return tr元素
     */
    @Override
    public List<String> getTrsFromTbody(String text) {
        //快速切割出课表所在的字符串
        Integer id1,id2,id3;
        id1=text.indexOf(beginTag1,0);
        id2=text.indexOf(beginTag2,id1);
        id3=text.indexOf(beginTag3,id2);
        Integer tbodyEnd=text.indexOf(endTagTbody,id3);
        List<String> trs = new ArrayList<>();
        //分离出tbody，也就是课表所在的标签
        String trString=text.substring(id3,tbodyEnd);
        Integer be=0,ed=0;
        for(int i=0;i<5;i++){
            be=trString.indexOf("<tr>",ed);
            ed=trString.indexOf("</tr>",be);
            trs.add(deleteAllBlank(trString.substring(be+4,ed)));
        }
        return trs;
    }
}
